package com.myjunit.xstream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;

import com.thoughtworks.xstream.XStream;

/**
 * XStream 简单示例
 * @author xysuncdl@cn.ibm.com
 * @version 2011-9-8
 */
public class XStreamTest {
	
	/**
	 * 对象转文件,文件转回对象
	 */
	@Test
	public void testFromFile() throws IOException {

		String fileName = "d:/personBObj.xml";
		OutputStream out = null;
		InputStream input = null;
		File file = new File(fileName);
		XStream xstream = initXStream();
		PersonBObj personBObj = personBObjInit();

		try {
			// 对象生成XML文件
			out = new FileOutputStream(fileName);
			xstream.toXML(personBObj, out);
			Assert.assertTrue(file.exists());

			// XML文件转对象
			input = new FileInputStream(fileName);
			PersonBObj fromFileBObj = (PersonBObj) xstream.fromXML(input);
			Assert.assertEquals("1", fromFileBObj.getContId());

		} finally {
			out.close();
			input.close();
		}

		if (file.exists()) {
			file.delete();
		}
	}

	/**
	 * 对象转String, String转回对象
	 */
	@Test
	public void testFromString() {

		XStream xStream = initXStream();

		// 对象转XML
		PersonBObj personBObj = personBObjInit();
		String xml = xStream.toXML(personBObj);
		Assert.assertNotNull(xml);

		// XML转对象
		PersonBObj fromXMLObj = (PersonBObj) xStream.fromXML(xml);
		Assert.assertEquals("1", fromXMLObj.getContId());
	}

	/**
	 * 初始化 XStream 对象
	 */
	private XStream initXStream() {

		XStream xstream = new XStream();

		// 以下为对象属性起别名
		xstream.alias("OrgNameBObj", OrgNameBObj.class);
		xstream.alias("ContactBObj", ContactBObj.class);
		xstream.alias("IdentifierBObj", IdentifierBObj.class);
		xstream.alias("ConteQuivBObj", ConteQuivBObj.class);
		xstream.alias("AddressBObj", AddressBObj.class);
		xstream.alias("ContactMethodBObj", ContactMethodBObj.class);
		xstream.alias("RelativePersonBObj", RelativePersonBObj.class);
		xstream.alias("OrgBObj", OrgBObj.class);
		xstream.alias("PersonBObj", PersonBObj.class);
		xstream.alias("PersonNameBObj", PersonNameBObj.class);

		xstream.aliasField("OrgNameBObjs", OrgBObj.class, "orgNameBObjs");
		xstream.aliasField("IdentifierBObjs", OrgBObj.class, "identifierBObjs");
		xstream.aliasField("ContEquivBObjs", OrgBObj.class, "contEquivBObjs");
		xstream.aliasField("AddressBObjs", OrgBObj.class, "addressBObjs");
		xstream.aliasField("ContactMethodBObjs", OrgBObj.class, "contactMethodBObjs");
		xstream.aliasField("RelativePersonBObjs", OrgBObj.class, "relativePersonBObjs");

		xstream.aliasField("PersonNameBObjs", PersonBObj.class, "personNameBObjs");
		xstream.aliasField("IdentifierBObjs", PersonBObj.class, "identifierBObjs");
		xstream.aliasField("ContEquivBObjs", PersonBObj.class, "contEquivBObjs");
		xstream.aliasField("AddressBObjs", PersonBObj.class, "addressBObjs");
		xstream.aliasField("ContactMethodBObjs", PersonBObj.class, "contactMethodBObjs");

		return xstream;
	}

	/**
	 * PersonBObj 对象全字段 
	 * stbtools 自动生成
	 */
	private PersonBObj personBObjInit() {

		PersonBObj object = new PersonBObj();
		object.setRownum("1");
		object.setContId("1");
		object.setCreateDt(new Date());
		object.setContactName("1");
		object.setInactivatedDt(new Date());
		object.setPersonOrgCode("1");
		object.setSolicitInd("1");
		object.setConfidentialInd("1");
		object.setClientImpTpCd(1);
		object.setClientStTpCd(1);
		object.setClientPotenTpCd(1);
		object.setSinceDt(new Date());
		object.setLeftDt(new Date());
		object.setSourceIdentTpCd(1);
		object.setCredLevelTpCd(1);
		object.setCredLevelDt(new Date());
		object.setPrefLangTpCd(1);
		object.setManagecom("1");
		object.setOperatecom("1");
		object.setMakedate(new Date());
		object.setGrpgroupno("1");
		object.setBlacklistflag("1");
		object.setVipvalue("1");
		object.setVipLevelTpCd(1);
		object.setRemark("1");
		object.setStatusTpCd(1);
		object.setIssuspicion("1");
		object.setSuspicionContId("1");
		object.setInputopt("1");
		object.setInputnode("1");
		object.setInputbranchcom("1");
		object.setInputDt(new Date());
		object.setModiopt("1");
		object.setModinode("1");
		object.setModibranchcom("1");
		object.setModiDt(new Date());
		object.setLastUpdateDt(new Date());
		object.setLastUpdateUser("1");
		object.setLastUpdateTxId(1);
		object.setLastUpdateBranchcom("1");

		object.setContId("1");
		object.setMaritalStTpCd(1);
		object.setBirthplaceTpCd(1);
		object.setSexTpCd(1);
		object.setBirthDt(new Date());
		object.setDeceasedDt(new Date());
		object.setChildrenCt(1);
		object.setDisabStartDt(new Date());
		object.setDisabEndDt(new Date());
		object.setUserInd("1");
		object.setNationalityTpCd(1);
		object.setNativeplace("1");
		object.setPoliticalFace("1");
		object.setUnitkind("1");
		object.setWorkcorp("1");
		object.setWorkbegindate(new Date());
		object.setDepartName("1");
		object.setDutyName("1");
		object.setOccupationTpCd(1);
		object.setPosition("1");
		object.setStaffFlag("1");
		object.setFamilystatus(1);
		object.setHighestDegreeTpCd(1);
		object.setWorkingAge(1);
		object.setOccuptionCatalogTpCd(1);
		object.setFirstWorkDt(new Date());
		object.setRetireDt(new Date());
		object.setRetireAge(1);
		object.setYearSalary(1);
		object.setFirstHalfYearSalary(1);
		object.setOccusalary(1);
		object.setCountryTpCd(1);
		object.setHealthStTpCd(1);
		object.setIssmoke("1");
		object.setBloodTpCd(1);
		object.setDisappearanceDt(new Date());
		object.setFeedAuditNum(1);
		object.setFeedChildNum(1);
		object.setWorkStateTpCd(1);
		object.setIsdisappearance("1");
		object.setInteractChannelTpCd(1);
		object.setLastUpdateDt(new Date());
		object.setLastUpdateUser("1");
		object.setLastUpdateTxId(1);
		object.setLastUpdateBranchcom("1");

		// 名称
		PersonNameBObj personNameBObj = new PersonNameBObj();
		personNameBObj.setPersonNameId(1);
		personNameBObj.setContId("1");
		personNameBObj.setPrefixNameTpCd(1);
		personNameBObj.setPrefixDesc("1");
		personNameBObj.setNameUsageTpCd(1);
		personNameBObj.setGivenNameOne("1");
		personNameBObj.setGivenNameTwo("1");
		personNameBObj.setLastName("1");
		personNameBObj.setSuffixDesc("1");
		personNameBObj.setStartDt(new Date());
		personNameBObj.setEndDt(new Date());
		personNameBObj.setLastUsedDt(new Date());
		personNameBObj.setLastVerifiedDt(new Date());
		personNameBObj.setSourceIdentTpCd(1);
		personNameBObj.setLastUpdateDt(new Date());
		personNameBObj.setLastUpdateUser("1");
		personNameBObj.setLastUpdateTxId(1);
		personNameBObj.setLastUpdateBranchcom("1");

		PersonNameBObj personNameBObj2 = new PersonNameBObj();
		personNameBObj2.setPersonNameId(1);
		personNameBObj2.setContId("1");
		personNameBObj2.setPrefixNameTpCd(1);
		personNameBObj2.setPrefixDesc("1");
		personNameBObj2.setNameUsageTpCd(1);
		personNameBObj2.setGivenNameOne("1");
		personNameBObj2.setGivenNameTwo("1");
		personNameBObj2.setLastName("1");
		personNameBObj2.setSuffixDesc("1");
		personNameBObj2.setStartDt(new Date());
		personNameBObj2.setEndDt(new Date());
		personNameBObj2.setLastUsedDt(new Date());
		personNameBObj2.setLastVerifiedDt(new Date());
		personNameBObj2.setSourceIdentTpCd(1);
		personNameBObj2.setLastUpdateDt(new Date());
		personNameBObj2.setLastUpdateUser("1");
		personNameBObj2.setLastUpdateTxId(1);
		personNameBObj2.setLastUpdateBranchcom("1");

		List<PersonNameBObj> personNameBObjs = new ArrayList<PersonNameBObj>();
		personNameBObjs.add(personNameBObj2);
		personNameBObjs.add(personNameBObj);
		object.setPersonNameBObjs(personNameBObjs);

		// 证件
		IdentifierBObj identifierBObj = new IdentifierBObj();
		identifierBObj.setIdentifierId(1);
		identifierBObj.setIdStatusTpCd(1L);
		identifierBObj.setContId("1");
		identifierBObj.setIdTpCd(1L);
		identifierBObj.setIdTpCdName("1");
		identifierBObj.setRefNum("1");
		identifierBObj.setStartDt(new Date());
		identifierBObj.setEndDt(new Date());
		identifierBObj.setExpiryDt(new Date());
		identifierBObj.setIssueProvTpCd(1L);
		identifierBObj.setIssueAreaCode("1");
		identifierBObj.setIdentifierDesc("1");
		identifierBObj.setIssueLocation("1");
		identifierBObj.setSourceIdentTpCd(1L);
		identifierBObj.setLastVerifiedDt(new Date());
		identifierBObj.setIssueGovernmentTpCd(1L);
		identifierBObj.setValidStartDt(new Date());
		identifierBObj.setLastUpdateDt(new Date());
		identifierBObj.setLastUpdateUser("1");
		identifierBObj.setLastUpdateTxId(1);
		identifierBObj.setLastUsedDt(new Date());
		identifierBObj.setLastUpdateBranchcom("1");

		IdentifierBObj identifierBObj2 = new IdentifierBObj();
		identifierBObj2.setIdentifierId(1);
		identifierBObj2.setIdStatusTpCd(1L);
		identifierBObj2.setContId("1");
		identifierBObj2.setIdTpCd(1L);
		identifierBObj2.setIdTpCdName("1");
		identifierBObj2.setRefNum("1");
		identifierBObj2.setStartDt(new Date());
		identifierBObj2.setEndDt(new Date());
		identifierBObj2.setExpiryDt(new Date());
		identifierBObj2.setIssueProvTpCd(1L);
		identifierBObj2.setIssueAreaCode("1");
		identifierBObj2.setIdentifierDesc("1");
		identifierBObj2.setIssueLocation("1");
		identifierBObj2.setSourceIdentTpCd(1L);
		identifierBObj2.setLastVerifiedDt(new Date());
		identifierBObj2.setIssueGovernmentTpCd(1L);
		identifierBObj2.setValidStartDt(new Date());
		identifierBObj2.setLastUpdateDt(new Date());
		identifierBObj2.setLastUpdateUser("1");
		identifierBObj2.setLastUpdateTxId(1);
		identifierBObj2.setLastUsedDt(new Date());
		identifierBObj2.setLastUpdateBranchcom("1");

		List<IdentifierBObj> identifierBObjs = new ArrayList<IdentifierBObj>();
		identifierBObjs.add(identifierBObj2);
		identifierBObjs.add(identifierBObj);
		object.setIdentifierBObjs(identifierBObjs);

		// 交叉索引
		ConteQuivBObj conteQuivBObj = new ConteQuivBObj();
		conteQuivBObj.setContEquivId(1);
		conteQuivBObj.setContId("1");
		conteQuivBObj.setAdminSysTpCd(1);
		conteQuivBObj.setAdminClientId("1");
		conteQuivBObj.setDescription("1");
		conteQuivBObj.setLastUpdateDt(new Date());
		conteQuivBObj.setLastUpdateUser("1");
		conteQuivBObj.setLastUpdateTxId(1);
		conteQuivBObj.setLastUpdateBranchcom("1");

		ConteQuivBObj conteQuivBObj2 = new ConteQuivBObj();
		conteQuivBObj2.setContEquivId(1);
		conteQuivBObj2.setContId("1");
		conteQuivBObj2.setAdminSysTpCd(1);
		conteQuivBObj2.setAdminClientId("1");
		conteQuivBObj2.setDescription("1");
		conteQuivBObj2.setLastUpdateDt(new Date());
		conteQuivBObj2.setLastUpdateUser("1");
		conteQuivBObj2.setLastUpdateTxId(1);
		conteQuivBObj2.setLastUpdateBranchcom("1");

		List<ConteQuivBObj> contEquivBObjs = new ArrayList<ConteQuivBObj>();
		contEquivBObjs.add(conteQuivBObj2);
		contEquivBObjs.add(conteQuivBObj);
		object.setContEquivBObjs(contEquivBObjs);

		// 地址
		AddressBObj addressBObj = new AddressBObj();
		addressBObj.setAddrId(1);
		addressBObj.setCountryTpCd(1);
		addressBObj.setCountryTpCdName("1");
		addressBObj.setProvStateTpCd(1);
		addressBObj.setProvStateTpCdName("1");
		addressBObj.setResidenceTpCd(1);
		addressBObj.setAddrLineOne("1");
		addressBObj.setAddrLineTwo("1");
		addressBObj.setAddrLineThree("1");
		addressBObj.setCityName("1");
		addressBObj.setPostalCode("1");
		addressBObj.setAddrStandardInd("1");
		addressBObj.setCountyCode("1");
		addressBObj.setBuildingName("1");
		addressBObj.setStreetNumber("1");
		addressBObj.setStreetName("1");
		addressBObj.setCityTpCd(1);
		addressBObj.setCountyTpCd(1);
		addressBObj.setLastUpdateDt(new Date());
		addressBObj.setLastUpdateUser("1");
		addressBObj.setLastUpdateTxId(1);
		addressBObj.setLastUpdateBranchcom("1");

		addressBObj.setAddrGroupId(1);
		addressBObj.setAddrId(1);
		addressBObj.setContId("1");
		addressBObj.setPreferredInd("1");
		addressBObj.setStartDt(new Date());
		addressBObj.setEndDt(new Date());
		addressBObj.setAddrUsageTpCd(1);
		addressBObj.setAddrUsageTpCdName("1");
		addressBObj.setLastUpdateDt(new Date());
		addressBObj.setLastUpdateUser("1");
		addressBObj.setLastUpdateTxId(1);
		addressBObj.setSourceIdentTpCd(1);
		addressBObj.setLastUpdateBranchcom("1");

		AddressBObj addressBObj2 = new AddressBObj();
		addressBObj2.setAddrId(1);
		addressBObj2.setCountryTpCd(1);
		addressBObj2.setCountryTpCdName("1");
		addressBObj2.setProvStateTpCd(1);
		addressBObj2.setProvStateTpCdName("1");
		addressBObj2.setResidenceTpCd(1);
		addressBObj2.setAddrLineOne("1");
		addressBObj2.setAddrLineTwo("1");
		addressBObj2.setAddrLineThree("1");
		addressBObj2.setCityName("1");
		addressBObj2.setPostalCode("1");
		addressBObj2.setAddrStandardInd("1");
		addressBObj2.setCountyCode("1");
		addressBObj2.setBuildingName("1");
		addressBObj2.setStreetNumber("1");
		addressBObj2.setStreetName("1");
		addressBObj2.setCityTpCd(1);
		addressBObj2.setCountyTpCd(1);
		addressBObj2.setLastUpdateDt(new Date());
		addressBObj2.setLastUpdateUser("1");
		addressBObj2.setLastUpdateTxId(1);
		addressBObj2.setLastUpdateBranchcom("1");

		addressBObj2.setAddrGroupId(1);
		addressBObj2.setAddrId(1);
		addressBObj2.setContId("1");
		addressBObj2.setPreferredInd("1");
		addressBObj2.setStartDt(new Date());
		addressBObj2.setEndDt(new Date());
		addressBObj2.setAddrUsageTpCd(1);
		addressBObj2.setAddrUsageTpCdName("1");
		addressBObj2.setLastUpdateDt(new Date());
		addressBObj2.setLastUpdateUser("1");
		addressBObj2.setLastUpdateTxId(1);
		addressBObj2.setSourceIdentTpCd(1);
		addressBObj2.setLastUpdateBranchcom("1");

		List<AddressBObj> addressBObjs = new ArrayList<AddressBObj>();
		addressBObjs.add(addressBObj2);
		addressBObjs.add(addressBObj);
		object.setAddressBObjs(addressBObjs);

		// 联系方式
		ContactMethodBObj contactMethodBObj = new ContactMethodBObj();
		contactMethodBObj.setContactMethodId(1);
		contactMethodBObj.setContMethCatCd(1);
		contactMethodBObj.setContMethCatCdValue("1");
		contactMethodBObj.setRefNum("1");
		contactMethodBObj.setLastUpdateDt(new Date());
		contactMethodBObj.setLastUpdateUser("1");
		contactMethodBObj.setLastUpdateTxId(1);
		contactMethodBObj.setContMethStdInd("1");
		contactMethodBObj.setLabelName("1");
		contactMethodBObj.setLastUpdateBranchcom("1");

		contactMethodBObj.setContmethodGroupId(1);
		contactMethodBObj.setContactMethodId(1);
		contactMethodBObj.setContId("1");
		contactMethodBObj.setContMethTpCd(1);
		contactMethodBObj.setContMethTpCdValue("1");
		contactMethodBObj.setMethodStTpCd(1);
		contactMethodBObj.setMethodStTpCdValue("1");
		contactMethodBObj.setAttachAllowInd("1");
		contactMethodBObj.setTextOnlyInd("1");
		contactMethodBObj.setMessageSize("1");
		contactMethodBObj.setCommentDesc("1");
		contactMethodBObj.setLastUpdateDt(new Date());
		contactMethodBObj.setLastUpdateUser("1");
		contactMethodBObj.setLastUpdateTxId(1);
		contactMethodBObj.setPreferredInd("1");
		contactMethodBObj.setStartDt(new Date());
		contactMethodBObj.setEndDt(new Date());
		contactMethodBObj.setLastUpdateBranchcom("1");

		ContactMethodBObj contactMethodBObj2 = new ContactMethodBObj();
		contactMethodBObj2.setContactMethodId(1);
		contactMethodBObj2.setContMethCatCd(1);
		contactMethodBObj2.setContMethCatCdValue("1");
		contactMethodBObj2.setRefNum("1");
		contactMethodBObj2.setLastUpdateDt(new Date());
		contactMethodBObj2.setLastUpdateUser("1");
		contactMethodBObj2.setLastUpdateTxId(1);
		contactMethodBObj2.setContMethStdInd("1");
		contactMethodBObj2.setLabelName("1");
		contactMethodBObj2.setLastUpdateBranchcom("1");

		contactMethodBObj2.setContmethodGroupId(1);
		contactMethodBObj2.setContactMethodId(1);
		contactMethodBObj2.setContId("1");
		contactMethodBObj2.setContMethTpCd(1);
		contactMethodBObj2.setContMethTpCdValue("1");
		contactMethodBObj2.setMethodStTpCd(1);
		contactMethodBObj2.setMethodStTpCdValue("1");
		contactMethodBObj2.setAttachAllowInd("1");
		contactMethodBObj2.setTextOnlyInd("1");
		contactMethodBObj2.setMessageSize("1");
		contactMethodBObj2.setCommentDesc("1");
		contactMethodBObj2.setLastUpdateDt(new Date());
		contactMethodBObj2.setLastUpdateUser("1");
		contactMethodBObj2.setLastUpdateTxId(1);
		contactMethodBObj2.setPreferredInd("1");
		contactMethodBObj2.setStartDt(new Date());
		contactMethodBObj2.setEndDt(new Date());
		contactMethodBObj2.setLastUpdateBranchcom("1");

		List<ContactMethodBObj> contactMethodBObjs = new ArrayList<ContactMethodBObj>();
		contactMethodBObjs.add(contactMethodBObj2);
		contactMethodBObjs.add(contactMethodBObj);
		object.setContactMethodBObjs(contactMethodBObjs);

		return object;
	}
}
